package belf.migrate.engine.job.jdbc;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

public class MemPipe implements Pipe {
    private final String fullTableName;
    private final ArrayBlockingQueue<Object> queue;
    private Long total = 0L;
    private final AtomicLong read = new AtomicLong(0L);
    private final AtomicLong wrote = new AtomicLong(0L);

    public MemPipe(String fullTableName) {
        this.fullTableName = fullTableName;
        this.queue = new ArrayBlockingQueue<Object>(100);
    }


    @Override
    public String name() {
        return this.fullTableName;
    }

    @Override
    public void put(Object o) {
        try {
            this.queue.put(o);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Object pop() {
        return this.queue.poll();
    }

    @Override
    public void setTotal(long total) {
        this.total = total;
    }

    @Override
    public long getTotal() {
        return this.total;
    }

    @Override
    public void readCount(long count) {
        this.read.addAndGet(count);
    }

    @Override
    public long getRead() {
        return this.read.get();
    }

    @Override
    public long getCached() {
        return queue.size();
    }

    @Override
    public void wroteCount(long count) {
        this.wrote.addAndGet(count);
    }

    @Override
    public long getWrote() {
        return this.wrote.get();
    }
}
